package com.itlong.cloud.operate.service.impl;
import com.itlong.cloud.POJO.DTO.operate.OperateAppTypeDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateAppTypeGetPageDTO;
import com.itlong.cloud.POJO.PO.InterfacePO;
import com.itlong.cloud.POJO.PO.OperateAppTypePO;
import com.itlong.cloud.POJO.VO.operate.InterfaceVO;
import com.itlong.cloud.POJO.VO.operate.OperateAppTypeVO;
import com.itlong.cloud.operate.dao.IOperateAppTypeDao;
import com.itlong.cloud.operate.service.IOperateAppTypeService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * <desc>
 *      应用类型业务接口实现类
 * </desc>
 *
 * @createDate 2021-08-04
 */
@Service
public class OperateAppTypeServiceImpl implements IOperateAppTypeService {

    @Autowired
    IOperateAppTypeDao iOperateAppTypeDao;
    /**
    * <desc>
    *      获取分页列表
    * </desc>
    * @param appTypeGetPageDto
    * @return: com.itlong.cloud.utils.page.Page<com.itlong.cloud.POJO.VO.operate.OperateAppTypeVO>
    * @author LiuYu
    * @date: 2021-08-16
     **/
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateAppTypeVO> getAppTypeList(OperateAppTypeGetPageDTO appTypeGetPageDto) {


        //将DTO转换为Map params
        try {
            List<OperateAppTypeVO> operateAppTypeVOS = new ArrayList<>();
            Map<String, Object> params = SqlUtil.convertBeanToMapParams(appTypeGetPageDto);
            Page<OperateAppTypeVO> page = new Page<>();
            page.getPage().setPageSize(appTypeGetPageDto.getPageSize());
            page.getPage().setCurrentPage(appTypeGetPageDto.getCurrentPage());
            params.put("currentPage",page.getPage().getStartSlip());
            params.put("pageSize",page.getPage().getPageSize());
            //填充返回数据集
            for (OperateAppTypePO operateAppTypePO:iOperateAppTypeDao.getAppTypeList(params)){
                OperateAppTypeVO operateAppTypeVO = new OperateAppTypeVO();
                BeanUtils.copyProperties(operateAppTypePO,operateAppTypeVO);
                operateAppTypeVOS.add(operateAppTypeVO);
            }
            page.setRows(operateAppTypeVOS,iOperateAppTypeDao.getPageCount(params));
            return page;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //分页类默认pageSize为20
         return null;

    }

    /**
    * <desc>
    *      添加应用类型
    * </desc>
    * @param operateAppTypeDTO
    * @return: java.lang.Integer
    * @author LiuYu
    * @date: 2021-08-16
     **/
    @Override
    public Integer save(OperateAppTypeDTO operateAppTypeDTO) throws Exception {
        if(StringUtils.isBlank(operateAppTypeDTO.getAppTypeName()) || operateAppTypeDTO.getStatus()==null){
            throw  new DataAccessException("【缺少必要参数】");
        }
        Map<String, Object> params = new HashMap<>(SqlUtil.convertBeanToMapParams(operateAppTypeDTO));
        params.put("appTypeId",LogicIdUtil.bussinessId());
        params.put("appTypeNum",new Random().nextInt(90000)+10000);
        params.put("createTime",new Date());
        return iOperateAppTypeDao.save(params);
    }

    /**
    * <desc>
    *      修改应用类型
    * </desc>
    * @param operateAppTypeDTO
    * @return: java.lang.Integer
    * @author LiuYu
    * @date: 2021-08-16
     **/
    @Override
    public Integer update(OperateAppTypeDTO operateAppTypeDTO) throws Exception {
        if(StringUtils.isBlank(operateAppTypeDTO.getAppTypeId()) || StringUtils.isBlank(operateAppTypeDTO.getAppTypeName()) || operateAppTypeDTO.getStatus()==null){
            throw  new DataAccessException("【缺少必要参数】");
        }
        Map<String, Object> params = new HashMap<>(SqlUtil.convertBeanToMapParams(operateAppTypeDTO));
        return iOperateAppTypeDao.update(params);
    }

    /**
    * <desc>
    *      删除应用类型
    * </desc>
    * @param ids
    * @return: java.lang.Integer
    * @author LiuYu
    * @date: 2021-08-16
     **/
    @Override
    public Integer delete(String ids) {
        return iOperateAppTypeDao.delete(ids);
    }

    /**
    * <desc>
    *      获取接口类型
    * </desc>
    * @param appTypeId
    * @return: java.util.List<com.itlong.cloud.POJO.VO.operate.InterfaceVO>
    * @author LiuYu
    * @date: 2021-08-16
     **/
    @Override
    public List<InterfaceVO> getAllotInterfaces(String appTypeId) {

            List<InterfacePO> interfacePOS = iOperateAppTypeDao.getAllotInterfaces(appTypeId);
            List<InterfaceVO> interfaceVOS = new ArrayList<>();
            for(InterfacePO interfacePO :interfacePOS){
                InterfaceVO interfaceVO = new InterfaceVO();
                BeanUtils.copyProperties(interfacePO,interfaceVO);
                interfaceVOS.add(interfaceVO);
            }
            return interfaceVOS;


    }

    /**
    * <desc>
    *      提交分配接口
    * </desc>
    * @param appTypeId
    * @param ids
    * @return: java.lang.Integer
    * @author LiuYu
    * @date: 2021-08-16
     **/
    @Override
    public Integer commitAllotInterfaces(String appTypeId, String ids) {
        iOperateAppTypeDao.deleteAllotInterfaces(appTypeId);
        String[] interfaceIds=StringHandlerUtil.splitString(ids);
        Map<String,Object> params= new HashMap<>();
        params.put("interfaceIds",interfaceIds);
        params.put("createTime",new Date());
        params.put("appTypeId",appTypeId);
        params.put("status",new Integer(1));
        return iOperateAppTypeDao.commitAllotInterfaces(params);
    }
}

